// pages/index/nearbyTeams/serviceDetail/serviceDetail.js
import request from '../../../../utils/request'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    xiangmu:[],
    packageId:'',
    packageDetail: {
      packageName: '',
      price: 0,
      packageImage: '',
      serviceObject: '55岁以上的老人',
      introduction: '',
      serviceItems: [
        {
          id: 1,
          name: '高血压随访服务',
          times: '2次'
        },
        {
          id: 2,
          name: '日常随访',
          times: '不限次数'
        },
        {
          id: 3,
          name: '血糖测量',
          times: '不限次数'
        },
        {
          id: 4,
          name: '血压测量',
          times: '4次'
        }
      ],
      servicePeriod: '一年（可续签）'
    },
    institution: {},
    team: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        packageId:options.id
      })
      this.getPackageDetail(options.id);
      this.getXiangMu();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getXiangMu(){
    request({
      url: `item/item/list`,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          console.log(res.data.rows)
          this.setData({
            xiangmu:res.data.rows
          });
        }
      }
    });
  },
  getPackageDetail(id) {
    request({
      url: `package/package/${id}`,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            packageDetail: res.data.data
          });
          // 获取机构信息
          this.getInstitutionInfo(res.data.data.packageId);
          // 获取团队信息
          this.getTeamInfo(res.data.data.packageId);
        }
      }
    });
  },

  getInstitutionInfo() {
    request({
      url: `system/institution/list`,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            institution: res.data.rows[0]
          });
        }
      }
    });
  },

  getTeamInfo(teamId) {
    request({
      url: `system/team/id/${teamId}`,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            team: res.data.data
          });
        }
      }
    });
  },

  onInstitutionTap() {
    const { institutionId } = this.data.packageDetail;
    wx.navigateTo({
      url: `/pages/index/nearbyInstitutions/institutionDetail/institutionDetail?id=${institutionId}`
    });
  },

  onTeamTap() {
    const { teamId } = this.data.packageDetail;
    wx.navigateTo({
      url: `/pages/index/nearbyTeams/teamDetail/teamDetail?id=${teamId}`
    });
  },

  onBuyPackage() {
    wx.showModal({
      title: '提示',
      content: '确定要购买该服务包吗？',
      success: (res) => {
        if (res.confirm) {
          // 处理购买逻辑
          request({
            url: 'package/order/create',
            method: 'POST',
            data: {
              packageId: this.data.packageDetail.id,
              // 其他需要的参数
            },
            success: (result) => {
              if (result.data.code === 200) {
                wx.showToast({
                  title: '购买成功',
                  icon: 'success'
                });
                // 可以跳转到订单页面或其他页面
              }
            }
          });
        }
      }
    });
  }
})